
clear all

cd "$root"
use "Data/data_voter_candidate.dta"
cd "$output"

global FE "OperatorId t_block"

* Create weighting variables used in secondary specifications *
cap drop wgt_obs wgt_reg
gen wgt_obs = 1/obs
gen wgt_reg = reg/obs

*** Descriptive statistics *****************************************************
********************************************************************************

estpost summarize v_cand perf_pred pref_match if incumbent==1, listwise
est store A
estpost summarize v_cand perf_pred pref_match if top_chal==1, listwise
est store B

estpost summarize cand_comp_sure_d cand_issue_sure_d cand_comp_d cand_issue_d if incumbent==1, listwise
est store C
estpost summarize cand_comp_sure_d cand_issue_sure_d cand_comp_d cand_issue_d if top_chal==1, listwise
est store D

estpost summarize cand_radio cand_campaign_leaf cand_campaign_visit cand_vbuy if incumbent==1, listwise
est store E
estpost summarize cand_radio cand_campaign_leaf cand_campaign_visit cand_vbuy if top_chal==1, listwise
est store F

local titles "& \multicolumn{4}{c}{ \textbf{Incumbent} } & \multicolumn{4}{c}{ \textbf{Challengers} } \\"
local lines "\cmidrule(lr{1em}){2-5} \cmidrule(lr{1em}){6-9} "
local cols "& Mean & SD & Min & Max & Mean & SD & Min & Max \\"

esttab A B using "Table A3.tex", replace ///
	nonum cells("mean(fmt(2) label($ $)) sd(fmt(2) label($ $)) min(fmt(2) label($ $)) max(fmt(2) label($ $))") ///
	label nomtitles posthead("`titles'" "`lines'" "`cols'")  substitute(_ _) refcat(v_cand "\textbf{Table 6}", nolabel) b(3) se f nogaps booktabs sfmt(3 %9.0fc) noobs compress
esttab C D using "Table A3.tex", append ///
	nonum cells("mean(fmt(2) label($ $)) sd(fmt(2) label($ $)) min(fmt(2) label($ $)) max(fmt(2) label($ $))") ///
	label nomtitles substitute(_ _) refcat(cand_comp_sure_d "\textbf{Table 9}", nolabel) b(3) se f nogaps booktabs sfmt(3 %9.0fc) noobs compress
esttab E F using "Table A3.tex", append ///
	nonum cells("mean(fmt(2) label($ $)) sd(fmt(2) label($ $)) min(fmt(2) label($ $)) max(fmt(2) label($ $))") ///
	label nomtitles substitute(_ _) refcat(cand_radio "\textbf{Table 10}", nolabel) b(3) se f nogaps booktabs sfmt(3 %9.0fc) noobs compress

	
*** Effects on campaigning *****************************************************
********************************************************************************

estimates drop _all

* Index outcomes *

global ctrls "c.r_survey_day c.r_male c.r_age c.party_UP c.r_ed_* c.ED_*" 
global keep_vars "r_survey_day r_male r_age party_UP r_ed_* 1.t_invite ED_*"

foreach var in i_std_cand_ground c_std_cand_ground i_std_cand_radio c_std_cand_radio {
	eststo `var': cap qui reghdfe `var' i.t_invite $ctrls, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		quietly qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w1: cap qui reghdfe `var'_w1 i.t_invite $ctrls [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		quietly qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w2: cap qui reghdfe `var'_w2 i.t_invite $ctrls [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		quietly qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w3: cap qui reghdfe `var'_w3 i.t_invite $ctrls [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		quietly qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
}

*** Effects on campaigning ***
local titles "& \multicolumn{3}{c}{ \textbf{Ground} } & \multicolumn{3}{c}{ \textbf{Radio} } \\"
local lines "\cmidrule(lr{1em}){2-4} \cmidrule(lr{1em}){5-7} "
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule"
esttab i_std_cand_ground i_std_cand_ground_w1 i_std_cand_ground_w2 i_std_cand_radio i_std_cand_radio_w1 i_std_cand_radio_w2 using "Table 10.tex", ///
	nonumber replace posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
esttab c_std_cand_ground c_std_cand_ground_w1 c_std_cand_ground_w2 c_std_cand_radio c_std_cand_radio_w1 c_std_cand_radio_w2 using "Table 10.tex", ///
	nonumber append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)

*** Effects on campaigning (with control coefficients) ***
local titles "& \multicolumn{6}{c}{ \textbf{Ground} } & \multicolumn{6}{c}{ \textbf{Radio} } \\"
local lines "\cmidrule(lr{1em}){2-7} \cmidrule(lr{1em}){8-13} "
local numbers " & \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{2}{c}{(3)} & \multicolumn{2}{c}{(4)} & \multicolumn{2}{c}{(5)} & \multicolumn{2}{c}{(6)} \\ \midrule"
esttab i_std_cand_ground i_std_cand_ground_w1 i_std_cand_ground_w2 i_std_cand_radio i_std_cand_radio_w1 i_std_cand_radio_w2 using "Table A35.tex", ///
	nonumber wide replace posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep($keep_vars)
esttab c_std_cand_ground c_std_cand_ground_w1 c_std_cand_ground_w2 c_std_cand_radio c_std_cand_radio_w1 c_std_cand_radio_w2 using "Table A35.tex", ///
	nonumber wide append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep($keep_vars)
	
*** Effects on campaigning (demography-weighted) ***
local titles "& \multicolumn{2}{c}{ \textbf{Ground} } & \multicolumn{2}{c}{ \textbf{Radio} } \\"
local lines "\cmidrule(lr{1em}){2-3} \cmidrule(lr{1em}){4-5} "
local numbers " & (1) & (2) & (3) & (4) \\ \midrule" 
esttab i_std_cand_ground i_std_cand_ground_w3 i_std_cand_radio i_std_cand_radio_w3 using "Table A17.tex", ///
	nonumber replace posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
esttab c_std_cand_ground c_std_cand_ground_w3 c_std_cand_radio c_std_cand_radio_w3 using "Table A17.tex", ///
	nonumber append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)

* Disaggregated outcomes *
	
foreach var in cand_vbuy cand_campaign_leaf cand_campaign_visit { 
	eststo `var': cap qui reghdfe `var' i.t_invite $ctrls if incumbent==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w1: cap qui reghdfe `var' i.t_invite $ctrls [aw=wgt_obs] if incumbent==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w2: cap qui reghdfe `var' i.t_invite $ctrls [aw=wgt_reg] if incumbent==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	eststo c_`var': cap qui reghdfe `var' i.t_invite  $ctrls if top_chal==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_w1: cap qui reghdfe `var' i.t_invite $ctrls [aw=wgt_obs] if top_chal==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_w2: cap qui reghdfe `var' i.t_invite $ctrls [aw=wgt_reg] if top_chal==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'		
}

*** Effects on campaigning (disaggregated) ***

local numbers " & (1) & (2) & (3) \\ \midrule"
local index "\multicolumn{4}{c}{\textbf{A. Incumbent}} \\ \midrule" 
esttab cand_campaign_visit cand_campaign_visit_w1 cand_campaign_visit_w2 using "Table A19.tex", ///
	nonumber replace posthead("`numbers'" "`index'") refcat(1.t_invite "\specialcell{\textbf{(1) Candidate visited community}}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
esttab cand_campaign_leaf cand_campaign_leaf_w1 cand_campaign_leaf_w2 using "Table A19.tex", ///
	nonumber append refcat(1.t_invite "\textbf{(2) Candidate distributed leaflets}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
esttab cand_vbuy cand_vbuy_w1 cand_vbuy_w2 using "Table A19.tex", ///
	nonumber append refcat(1.t_invite "\textbf{(3) Vote buying for candidate}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
local index "\midrule \multicolumn{4}{c}{\textbf{B. Challenger}} \\ \midrule" 
esttab c_cand_campaign_visit c_cand_campaign_visit_w1 c_cand_campaign_visit_w2 using "Table A19.tex", ///
	append nonumber posthead("`index'") refcat(1.t_invite "\textbf{(1) Candidate visited community}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
esttab c_cand_campaign_leaf c_cand_campaign_leaf_w1 c_cand_campaign_leaf_w2 using "Table A19.tex", ///
	append nonumber refcat(1.t_invite "\textbf{(2) Candidate distributed leaflets}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
esttab c_cand_vbuy c_cand_vbuy_w1 c_cand_vbuy_w2 using "Table A19.tex", ///
	append nonumber refcat(1.t_invite "\textbf{(3) Vote buying for candidate}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invite)
	
	
*** Effects on updating about candidates ***************************************
********************************************************************************

estimates drop _all

global ctrls "c.r_survey_day c.r_male c.r_age c.party_UP c.r_ed_* c.ED_* i.rb_treat_bl##i.t_invite"

foreach var in i_std_cand_comp_sure_d c_std_cand_comp_sure_d i_std_cand_issue_sure_d c_std_cand_issue_sure_d i_std_cand_comp_d c_std_cand_comp_d i_std_cand_issue_d c_std_cand_issue_d {
	eststo `var': cap qui reghdfe `var' i.t_invite $ctrls, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w1: cap qui reghdfe `var'_w1 i.t_invite $ctrls [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w2: cap qui reghdfe `var'_w2 i.t_invite $ctrls [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo `var'_w3: cap qui reghdfe `var'_w3 i.t_invite $ctrls [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
}

*** Effects on updating about candidates ***
local titles "& \multicolumn{3}{c}{ \textbf{Certainty about competence} } & \multicolumn{3}{c}{ \textbf{Certainty about policy} } \\"
local lines "\cmidrule(lr{1em}){2-4} \cmidrule(lr{1em}){5-7} "
local numbers "\textbf{1. Uncertainty} & (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule"
esttab i_std_cand_comp_sure_d i_std_cand_comp_sure_d_w1 i_std_cand_comp_sure_d_w2 i_std_cand_issue_sure_d i_std_cand_issue_sure_d_w1 i_std_cand_issue_sure_d_w2 using "Table 9.tex", ///
	nonumber replace posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
esttab c_std_cand_comp_sure_d c_std_cand_comp_sure_d_w1 c_std_cand_comp_sure_d_w2 c_std_cand_issue_sure_d c_std_cand_issue_sure_d_w1 c_std_cand_issue_sure_d_w2 using "Table 9.tex", ///
	nonumber append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
local titles "\midrule \\ & \multicolumn{3}{c}{ \textbf{Beliefs about competence} } & \multicolumn{3}{c}{ \textbf{Learning about policy} } \\"
local lines "\cmidrule(lr{1em}){2-4} \cmidrule(lr{1em}){5-7} "
local numbers "\textbf{2. Levels} & (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule"
esttab i_std_cand_comp_d i_std_cand_comp_d_w1 i_std_cand_comp_d_w2 i_std_cand_issue_d i_std_cand_issue_d_w1 i_std_cand_issue_d_w2 using "Table 9.tex", ///
	nonumber append posthead("`space'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
esttab c_std_cand_comp_d c_std_cand_comp_d_w1 c_std_cand_comp_d_w2 c_std_cand_issue_d c_std_cand_issue_d_w1 c_std_cand_issue_d_w2 using "Table 9.tex", ///
	nonumber append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)

*** Effects on updating about candidates (with control coefficients) ***
local titles "& \multicolumn{6}{c}{ \textbf{Certainty about competence} } & \multicolumn{6}{c}{ \textbf{Certainty about policy} } \\"
local lines "\cmidrule(lr{1em}){2-7} \cmidrule(lr{1em}){8-13} "
local numbers "\textbf{1. Uncertainty} & \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{2}{c}{(3)} & \multicolumn{2}{c}{(4)} & \multicolumn{2}{c}{(5)} & \multicolumn{2}{c}{(6)} \\ \midrule"
esttab i_std_cand_comp_sure_d i_std_cand_comp_sure_d_w1 i_std_cand_comp_sure_d_w2 i_std_cand_issue_sure_d i_std_cand_issue_sure_d_w1 i_std_cand_issue_sure_d_w2 using "Table A33.tex", ///
	nonumber replace wide posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep($keep_vars)
esttab c_std_cand_comp_sure_d c_std_cand_comp_sure_d_w1 c_std_cand_comp_sure_d_w2 c_std_cand_issue_sure_d c_std_cand_issue_sure_d_w1 c_std_cand_issue_sure_d_w2 using "Table A33.tex", ///
	nonumber append wide refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep($keep_vars)
local titles "& \multicolumn{6}{c}{ \textbf{Beliefs about competence} } & \multicolumn{6}{c}{ \textbf{Learning about policy} } \\"
local lines "\cmidrule(lr{1em}){2-7} \cmidrule(lr{1em}){8-13} "
local numbers "\textbf{2. Levels} & \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{2}{c}{(3)} & \multicolumn{2}{c}{(4)} & \multicolumn{2}{c}{(5)} & \multicolumn{2}{c}{(6)} \\ \midrule"
esttab i_std_cand_comp_d i_std_cand_comp_d_w1 i_std_cand_comp_d_w2 i_std_cand_issue_d i_std_cand_issue_d_w1 i_std_cand_issue_d_w2 using "Table A34.tex", ///
	nonumber replace wide posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep($keep_vars)
esttab c_std_cand_comp_d c_std_cand_comp_d_w1 c_std_cand_comp_d_w2 c_std_cand_issue_d c_std_cand_issue_d_w1 c_std_cand_issue_d_w2 using "Table A34.tex", ///
	nonumber append wide refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep($keep_vars)

*** Effects on updating about candidates (demography-weighted) ***
local titles "& \multicolumn{2}{c}{ \specialcell{ \textbf{Certainty about} \\ \textbf{competence} }} & \multicolumn{2}{c}{ \specialcell{\textbf{Certainty about} \\ \textbf{policy} }} \\"
local lines "\cmidrule(lr{1em}){2-3} \cmidrule(lr{1em}){4-5} "
local numbers "\textbf{1. Uncertainty} & (1) & (2) & (3) & (4) \\ \midrule"
esttab i_std_cand_comp_sure_d i_std_cand_comp_sure_d_w3 i_std_cand_issue_sure_d i_std_cand_issue_sure_d_w3 using "Table A16.tex", ///
	nonumber replace posthead("`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
esttab c_std_cand_comp_sure_d c_std_cand_comp_sure_d_w3 c_std_cand_issue_sure_d c_std_cand_issue_sure_d_w3 using "Table A16.tex", ///
	nonumber append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
local titles "\midrule \\ & \multicolumn{2}{c}{ \specialcell{\textbf{Beliefs about} \\ \textbf{competence} }} & \multicolumn{2}{c}{ \specialcell{\textbf{Learning about} \\ \textbf{policy} }} \\"
local lines "\cmidrule(lr{1em}){2-3} \cmidrule(lr{1em}){4-5} "
local numbers "\textbf{2. Levels} & (1) & (2) & (3) & (4) \\ \midrule" 
esttab i_std_cand_comp_d i_std_cand_comp_d_w3 i_std_cand_issue_d i_std_cand_issue_d_w3 using "Table A16.tex", ///
	nonumber append posthead("`space'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
esttab c_std_cand_comp_d c_std_cand_comp_d_w3 c_std_cand_issue_d c_std_cand_issue_d_w3 using "Table A16.tex", ///
	nonumber append refcat(1.t_invite "\textbf{B. Challengers}", nolabel) b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(%9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invite)
	
	
*** Effects on voting outcomes *************************************************
********************************************************************************

*** NOTE: This only produces the top panel of Table 6. Run make_tables_ps.do to produce the bottom panel ***

global ctrls "c.r_survey_day c.r_male c.r_age c.r_ed_* c.party_UP c.ED_* c.std_perf c.std_match i.t_invite##i.v_cand_bl"

foreach var in v_cand v_cand_d {

	cap drop std_perf std_match
	
	* Incumbent-level analysis *
	
	egen std_perf=std(perf_pred) if incumbent==1
	egen std_match=std(pref_match) if incumbent==1
	
	eststo i_`var': capture quietly reghdfe `var' i.t_invite $ctrls if incumbent == 1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_w1: capture quietly reghdfe `var' i.t_invite $ctrls if incumbent == 1 [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_w2: capture quietly reghdfe `var' i.t_invite $ctrls if incumbent == 1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_w3: capture quietly reghdfe `var' i.t_invite $ctrls if incumbent == 1 [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	eststo i_`var'_i: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if incumbent == 1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i_w1: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if incumbent == 1 [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo i_`var'_i_w2: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if incumbent == 1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i_w3: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if incumbent == 1 [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	
	eststo i_`var'_i1: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if incumbent == 1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i1_w1: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if incumbent == 1 [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo i_`var'_i1_w2: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if incumbent == 1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo i_`var'_i1_w3: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if incumbent == 1 [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	

	* Challenger-level analysis *

	cap drop std_perf std_match
	
	egen std_perf=std(perf_pred) if top_chal==1
	label variable std_perf "\hspace{0.1cm}$\widehat{\text{Performance}}$"
	egen std_match=std(pref_match) if top_chal==1
	label variable std_match "\hspace{0.1cm}Policy alignment"		
	
	eststo c_`var': capture quietly reghdfe `var' i.t_invite $ctrls if top_chal == 1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_w1: capture quietly reghdfe `var' i.t_invite $ctrls if top_chal == 1 [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo c_`var'_w2: capture quietly reghdfe `var' i.t_invite $ctrls if top_chal == 1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_w3: capture quietly reghdfe `var' i.t_invite $ctrls if top_chal == 1 [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	eststo c_`var'_i: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if top_chal == 1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo c_`var'_i_w1: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if top_chal == 1 [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo c_`var'_i_w2: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if top_chal == 1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_i_w3: capture quietly reghdfe `var' i.t_invite##c.std_perf $ctrls if top_chal == 1 [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
				
	eststo c_`var'_i1: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if top_chal == 1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'	
	eststo c_`var'_i1_w1: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if top_chal == 1 [aw=wgt_obs], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/Obs"
		qui sum `var' [aw=wgt_obs] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'		
	eststo c_`var'_i1_w2: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if top_chal == 1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg/Obs"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_i1_w3: capture quietly reghdfe `var' i.t_invite##c.std_match $ctrls if top_chal == 1 [aw=wgt_dem], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Dem"
		qui sum `var' [aw=wgt_dem] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
}		

*** Effects on voting outcomes (Panel A) ***

local super_title " & & & & \multicolumn{6}{c}{ \textbf{Interaction term:} } \\"
local titles "& \multicolumn{3}{c}{ \textbf{Main effect} } & \multicolumn{3}{c}{ \textbf{Std. Performance} }  & \multicolumn{3}{c}{ \textbf{Std. Policy alignment} } \\"
local lines "\cmidrule(lr{0.5em}){2-4} \cmidrule(lr{0.5em}){5-7} \cmidrule(lr{0.5em}){8-10}"
local numbers "\textbf{1. Respondent-level} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\ \midrule"
esttab i_v_cand i_v_cand_w1 i_v_cand_w2 i_v_cand_i i_v_cand_i_w1 i_v_cand_i_w2 i_v_cand_i1 i_v_cand_i1_w1 i_v_cand_i1_w2 using "Table 6.tex", ///
	replace nonumber posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) interaction(" $\times$") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit*) drop(*v_cand*)
esttab c_v_cand c_v_cand_w1 c_v_cand_w2 c_v_cand_i c_v_cand_i_w1 c_v_cand_i_w2 c_v_cand_i1 c_v_cand_i1_w1 c_v_cand_i1_w2 using "Table 6.tex", ///
	append nonumber refcat(1.t_invite "\textbf{B. Challengers}", nolabel) interaction(" $\times$") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit*) drop(*v_cand*)
	
*** Effects on voting outcomes (supplementary) ***
esttab i_v_cand_d i_v_cand_d_w1 i_v_cand_d_w2 i_v_cand_d_i i_v_cand_d_i_w1 i_v_cand_d_i_w2 i_v_cand_d_i1 i_v_cand_d_i1_w1 i_v_cand_d_i1_w2 using "Table A23.tex", ///
	replace nonumber posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) interaction(" $\times$") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit*) drop(*v_cand*)
esttab c_v_cand_d c_v_cand_d_w1 c_v_cand_d_w2 c_v_cand_d_i c_v_cand_d_i_w1 c_v_cand_d_i_w2 c_v_cand_d_i1 c_v_cand_d_i1_w1 c_v_cand_d_i1_w2 using "Table A23.tex", ///
	append nonumber refcat(1.t_invite "\textbf{B. Challengers}", nolabel) interaction(" $\times$") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit*) drop(*v_cand*)

*** Effects on voting outcomes (Panel A) (with control coefficients) ***
local super_title " & & \multicolumn{6}{c}{ } & \multicolumn{6}{c}{ \textbf{Interaction term:} } \\"
local titles "& \multicolumn{6}{c}{ \textbf{Main effect} } & \multicolumn{6}{c}{ \textbf{Std. Performance} }  & \multicolumn{6}{c}{ \textbf{Std. Policy alignment} } \\"
local lines "\cmidrule(lr{0.5em}){2-7} \cmidrule(lr{0.5em}){8-13} \cmidrule(lr{0.5em}){14-19}"
local numbers "\textbf{1. Respondent-level} & \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{2}{c}{(3)} & \multicolumn{2}{c}{(4)} & \multicolumn{2}{c}{(5)} & \multicolumn{2}{c}{(6)} & \multicolumn{2}{c}{(7)} & \multicolumn{2}{c}{(8)} & \multicolumn{2}{c}{(9)} \\ \midrule"
esttab i_v_cand i_v_cand_w1 i_v_cand_w2 i_v_cand_i i_v_cand_i_w1 i_v_cand_i_w2 i_v_cand_i1 i_v_cand_i1_w1 i_v_cand_i1_w2 using "Table A27.tex", ///
	replace nonumber wide posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) interaction(" $\times$ ") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit* $keep_vars ) order(1.t_invit*)  drop(*v_cand*)
esttab c_v_cand c_v_cand_w1 c_v_cand_w2 c_v_cand_i c_v_cand_i_w1 c_v_cand_i_w2 c_v_cand_i1 c_v_cand_i1_w1 c_v_cand_i1_w2 using "Table A28.tex", ///
	replace nonumber wide posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{B. Challengers}", nolabel) interaction(" $\times$ ") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit* $keep_vars ) order(1.t_invit*) drop(*v_cand*)

*** Effects on voting outcomes (Panel A) (demography-weighted) ***
local super_title " & & & \multicolumn{4}{c}{ \textbf{Interaction term:} } \\"
local titles "& \multicolumn{2}{c}{ \textbf{Main effect} } & \multicolumn{2}{c}{ \textbf{Std. Performance} }  & \multicolumn{2}{c}{ \textbf{Std. Policy alignment} } \\"
local lines "\cmidrule(lr{0.5em}){2-3} \cmidrule(lr{0.5em}){4-5} \cmidrule(lr{0.5em}){6-7}"
local numbers "\textbf{1. Respondent-level} & (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule"
esttab i_v_cand i_v_cand_w3 i_v_cand_i i_v_cand_i_w3 i_v_cand_i1 i_v_cand_i1_w3 using "Table A14.tex", ///
	replace nonumber posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) interaction(" $\times$ ") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit*)  drop(*v_cand*)
esttab c_v_cand c_v_cand_w3 c_v_cand_i c_v_cand_i_w3 c_v_cand_i1 c_v_cand_i1_w3 using "Table A14.tex", ///
	append nonumber refcat(1.t_invite "\textbf{B. Challengers}", nolabel) interaction(" $\times$ ") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels  keep(1.t_invit*) drop(*v_cand*)	
	
	